package edu.tjufe.llp.controller;

import java.net.URLEncoder;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.tjufe.llp.common.Result;
import edu.tjufe.llp.entity.ChargeArea;
import edu.tjufe.llp.entity.Station;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import edu.tjufe.llp.entity.ChargeRecord;
import edu.tjufe.llp.service.IChargeRecordService;

/**
 * 充电记录Controller
 * 
 * @author llp
 * @date 2022-12-25
 */
@RestController
@RequestMapping("/record")
public class ChargeRecordController
{
    @Autowired
    private IChargeRecordService chargeRecordService;

    /**
     * 查询充电记录列表
     */
    @GetMapping("/list")
//    public Result getRecordList(@RequestParam Integer pageNum,
//                              @RequestParam Integer pageSize){
//        IPage<ChargeRecord> page = new Page<>(pageNum,pageSize);
//        QueryWrapper<ChargeRecord> queryWrapper = new QueryWrapper<>();
////        queryWrapper.like("area_name",areaName);
//        return Result.success(chargeRecordService.page(page,queryWrapper));
//    }
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) Integer carId,
                           @RequestParam(required = false) Integer stationId
    ){
        Page<ChargeRecord> page = chargeRecordService.findPage(new Page<>(pageNum,pageSize),carId,stationId);
        return Result.success(page);
    }

    @GetMapping("/recordTotal")
    public Result findAll(ChargeRecord chargeRecord) {
        List<ChargeRecord> list = chargeRecordService.selectChargeRecordList(chargeRecord);
        return Result.success(list);
    }

    /**
     * 导出充电记录列表
     */
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, ChargeRecord chargeRecord)
//    {
//        List<ChargeRecord> list = chargeRecordService.selectChargeRecordList(chargeRecord);
//        ExcelUtil<ChargeRecord> util = new ExcelUtil<ChargeRecord>(ChargeRecord.class);
//        util.exportExcel(response, list, "充电记录数据");
//    }
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        // 从数据库查询出所有的数据
        List<ChargeRecord> list = chargeRecordService.list();

        // 通过工具类创建writer 写出到磁盘路径
//        ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath + "/用户信息.xlsx");
        // 在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义标题别名
        writer.addHeaderAlias("recordId", "记录id");
        writer.addHeaderAlias("chargeStart", "开始充电时间");
        writer.addHeaderAlias("chargeEnd", "结束充电时间");
        writer.addHeaderAlias("money", "充电花费");
        writer.addHeaderAlias("recordRemark", "备注");

        // 一次性写出list内的对象到excel，使用默认样式，强制输出标题
        writer.write(list, true);

        // 设置浏览器响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("充电记录信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();

    }


    /**
     * 获取充电记录详细信息
     */
    @GetMapping(value = "/{recordId}")
    public Result getInfo(@PathVariable("recordId") Integer recordId)
    {
        return Result.success(chargeRecordService.selectChargeRecordByRecordId(recordId));
    }

    /**
     * 新增充电记录
     */
    @PostMapping
    public Result add(@RequestBody ChargeRecord chargeRecord)
    {
        return Result.toJudge(chargeRecordService.insertChargeRecord(chargeRecord));
    }

    /**
     * 修改充电记录
     */
    @PutMapping
    public Result edit(@RequestBody ChargeRecord chargeRecord)
    {
        return Result.toJudge(chargeRecordService.updateChargeRecord(chargeRecord));
    }

    /**
     * 删除充电记录
     */
	@DeleteMapping("/{recordIds}")
    public Result remove(@PathVariable Integer[] recordIds)
    {
        return Result.toJudge(chargeRecordService.deleteChargeRecordByRecordIds(recordIds));
    }
}
